/*

Frequency of use: [1] 2 3 4 5 LOW

Short:
    Interpreter processes commands and performs actions based on its input. Here 
we create a simple command language consisting of single characters

Long:
    In computer programming, the interpreter pattern is a particular design 
pattern. The interpreter pattern specifies how to evaluate sentences in a 
language. The basic idea is to have a class for each symbol (terminal or 
nonterminal) in a specialized computer language. The syntax tree of a sentence 
in the language is an instance of the composite pattern and is used to evaluate 
(interpret) the sentence.

Information sources:
 - http://calumgrant.net/patterns/index.html
 - http://en.wikipedia.org/wiki/Interpreter_pattern
 - http://www.dofactory.com/Patterns/Patterns.aspx
*/

#include <string>
#include <iostream>
#include <stdexcept>

class Interpreter
{
public:
	template<typename It>
	void run(It from, It to)
	{
		for(It i=from; i!=to; ++i)
		{
			switch(*i)
			{
			case 'h': std::cout << "Hello";		break;
			case ' ': std::cout << ' ';			break;
			case 'w': std::cout << "world";		break;
			case '!': std::cout << '!';			break;
			case 'n': std::cout << std::endl;	break;
			default:  throw std::runtime_error("Unknown command");
			}
		}
	}
};

void hello_world(const std::string & script)
{
	Interpreter().run(script.begin(), script.end());
}

int main()
{
	hello_world("h w!n");
	return 0;
}

